<?php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use yii\web\Request;
use yii\web\Response;
use proxy\HttpProxy;
use helper\SessionHelper;

class BaseController extends Controller
{
    public $proxy = null;
    public $remote_params = [];
    public $return_page_values = [];
    public $enableCsrfValidation = false;    
    public $session = null;    
    public $headers = null;    
    public $auth = null; 
    

    public function beforeAction($action)
    {
        parent::beforeAction($action);
        $this->enableCsrfValidation = false;
       
        $this->headers = Yii::$app->response->headers; 
        $this->proxy = new HttpProxy(); 
        //初始化会话，获取admin_id 
        $this->session = new SessionHelper();
        $this->auth = Yii::$app->authManager;
        
        $this->remote_params = [
                                    'admin_id' => $this->session->get('admin_user_id')
                               ];
        $this->return_page_values = [
                                        'admin_name' => $this->session->get('admin_user_name')
                                    ];
        
        return true;
    }
 
    public function myRenderFile($file, $params = []) {

        $request = Yii::$app->request;
        $format = $request->get('format');
        if ($format != null && $format == 'json') {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return $params;
        }
 
        return $this->renderFile($file, $params);

    }

    public function myRedirect($pathUrl) {
        $request = Yii::$app->request;
        $format = $request->get('format');
        if ($format != null && $format == 'json') {
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ["redirect_to" => $pathUrl];
        }

        header('Location:'.Yii::$app->params['HOST'].$pathUrl);
        exit;
    }
    
}
